package com.njwb.Swing.Bill;

import com.njwb.Util.UtilService;
import com.njwb.pojo.Supplier;

import javax.swing.*;
import java.awt.*;
import java.sql.Date;
import java.util.ArrayList;

public class AddBillDialog extends JDialog {
    private JTextField productField;
    private JTextField quantityField;
    private JTextField unitField;
    private JTextField amountField;
    private JComboBox<String> isPaidBox;
    private JComboBox<String> supplierBox;
    private JTextField descriptionField;
    private JTextField dateField; // yyyy-mm-dd
    private JButton confirmButton;
    private JButton cancelButton;

    public AddBillDialog(Frame owner, UtilService utilService, Runnable refreshCallback) {
        super(owner, "新增账单", true);

        setSize(400, 450);
        setLocationRelativeTo(owner);
        setLayout(new GridLayout(9, 2, 5, 5));

        add(new JLabel("商品名称:"));
        productField = new JTextField();
        add(productField);

        add(new JLabel("交易数量:"));
        quantityField = new JTextField();
        add(quantityField);

        add(new JLabel("交易单位:"));
        unitField = new JTextField();
        add(unitField);

        add(new JLabel("交易金额:"));
        amountField = new JTextField();
        add(amountField);

        add(new JLabel("是否付款:"));
        isPaidBox = new JComboBox<>(new String[]{"未付款", "已付款"});
        add(isPaidBox);

        add(new JLabel("所属供应商:"));
        supplierBox = new JComboBox<>();
        add(supplierBox);

        add(new JLabel("商品描述:"));
        descriptionField = new JTextField();
        add(descriptionField);

        add(new JLabel("交易时间 (yyyy-mm-dd):"));
        dateField = new JTextField();
        add(dateField);

        confirmButton = new JButton("确认");
        cancelButton = new JButton("取消");
        add(confirmButton);
        add(cancelButton);

        // 填充供应商列表
        ArrayList<Supplier> suppliers = utilService.getAllSupplier();
        for (Supplier s : suppliers) {
            supplierBox.addItem(s.getName());
        }

        // 按钮事件
        confirmButton.addActionListener(e -> {
            try {
                String product_name = productField.getText().trim();
                int quantity = Integer.parseInt(quantityField.getText().trim());
                String unit = unitField.getText().trim();
                Double amount = Double.parseDouble(amountField.getText().trim());
                Boolean is_paid = isPaidBox.getSelectedIndex() == 1;
                String supplier_name = (String) supplierBox.getSelectedItem();
                String product_description = descriptionField.getText().trim();
                Date bill_time = Date.valueOf(dateField.getText().trim()); // yyyy-mm-dd

                boolean success = utilService.addBill(product_name, quantity, unit, amount,
                        is_paid, supplier_name, product_description, bill_time);
                if (success) {
                    JOptionPane.showMessageDialog(this, "新增账单成功！");
                    dispose();
                    if (refreshCallback != null) refreshCallback.run();
                } else {
                    JOptionPane.showMessageDialog(this, "新增账单失败！", "错误", JOptionPane.ERROR_MESSAGE);
                }
            } catch (Exception ex) {
                ex.printStackTrace(); // 输出到控制台
                JOptionPane.showMessageDialog(this, "输入有误，请检查数据格式！\n" + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            }

        });

        cancelButton.addActionListener(e -> dispose());
    }
}
